var QQMapWX = require('../libs/qqmap-wx-jssdk.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buthidden:false,
    currentLat:"",
    currentLon:"",
    address: "",
    detailAddress: "",
    location:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this ;
    //console.log(options);
    var allAddress = options.allAddress.split(" ");
    var address = allAddress[0] + ' ' + allAddress[1] + ' ' + allAddress[2] ;
    var detailAddress = allAddress[3] ;
    that.setData({ address: address, detailAddress: detailAddress});
  //  that.getLocationData();
    
  },
  //获取当前经纬度
  getLocationData: function () {
    var that = this ;
    wx.getLocation({
      //type: 'gcj02',
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var locationDic = { 'latitude': latitude, 'longitude': longitude };
        wx.setStorage({
          key: 'map_Location',
          data: locationDic,
        });
        that.setData({
          currentLat: latitude,
          currentLon: longitude,
        });
        
        var qqmapsdk = new QQMapWX({
          key: 'O5GBZ-HWGC3-UF63Q-YHQJJ-4BPY3-MMFUU'
        });
        // 调用接口
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: that.data.currentLat,
            longitude: that.data.currentLon
          },
          success: function (res) {
            console.log(res);
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            var address = res.result.address_component.province + ' ' + res.result.address_component.city + ' ' +res.result.address_component.district;
            var detailAddress = res.result.address_component.street;
            var location = res.result.location.lat + ',' + res.result.location.lng;
            that.setData({
              address: address,
              detailAddress: detailAddress,
              location: location
            });
          }
        });
      }
    })
  },
  
  // 选择省市区
  chooseAddress: function (e) {
    wx.navigateTo({
        url: '../mine/mapLocation/searchMapLocation/searchMapLocation',
    })
  },

 //提交按钮
  bindFormSubmit:function(e){
    var that = this;
    var address = e.detail.value.address;
    var detailAddress = e.detail.value.detailAddress;
    var province = (that.data.address).split(" ")[0];
    var city = (that.data.address).split(" ")[1];
    var area = (that.data.address).split(" ")[2];
    var lat = (that.data.location).split(",")[0];
    var lng = (that.data.location).split(",")[1];

    if (!address || !detailAddress){
      wx.showToast({
        title: '地址为空',
        icon: "none"
      })
      return false;
    } else if (city != "苏州市") {
      wx.showModal({
        title: '该地区暂不支持',
        confirmColor: '#56BDE0',
        showCancel: false,
      })
      return false;
    }else {
      that.setData({ buthidden: true });
      wx.request({
        method: "POST",
        url: 'https://www.zaishengwang.com/wx/toUpdateUser.do',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          id: app.globalData.userId,
          province: province,
          city: city,
          area: area,
          address: detailAddress,
          lat: lat,
          lng: lng
        },
        success: function (res) {
          var c = 2;
          that.setData({ buthidden: false });
          wx.showToast({
            title: '地址修改成功',
            icon: 'success',
            duration: 1500
          });
          var intervalId = setInterval(function () {
            c = c - 1;
            if (c == 0) {
              clearInterval(intervalId);
              wx.navigateBack({
                delta: 1
              })
            }
          }, 1000)
          
        },
        fail: function (error) {
          that.setData({ buthidden: false });
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})